<%@ page import="util.Decode" %>
<%@ page import="net.sf.json.JSONArray" %>
<%@ page import="net.sf.json.JSONObject" %>
<%@ page import="server.*" %>
<%@ page import="java.util.LinkedHashMap" %>
<%@ page import="config.ApplicationConfig" %>
<%@ page import="java.util.LinkedList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    LinkedHashMap<String, String> pageConfig = ApplicationConfig.readConfig("page.json");
    request.setCharacterEncoding(pageConfig.get("encode"));
    response.setCharacterEncoding(pageConfig.get("encode"));
    Object user = session.getAttribute("user");
%>
<html>
<head>
    <title>题 目</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/page.css">
    <link rel="stylesheet" href="css/subject.css">
    <link rel="stylesheet" href="css/highlight/default.css">
</head>
<body>
<nav class="navbar navbar-inverse head" role="navigation">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse"
                    data-target="#navbar">
                <span class="sr-only">切换导航</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <%
                SettingServer settingServer = new SettingServer();
                settingServer.open();
                JSONObject logo = settingServer.readSettingObject("logo");
                if (logo.getString("logo").equals("")) {
            %>
            <img class="navbar-brand" src="image/logo.png">
            <%
            } else {
                FileServer fileServer = new FileServer();
                fileServer.open();
            %>
            <img class="navbar-brand" src="<%=fileServer.getImage(logo.getString("logo"))%>">
            <%
                    fileServer.close();
                }
            %>
            <a class="navbar-brand" href="#"><%=logo.getString("name")%>
            </a>
        </div>
        <div class="collapse navbar-collapse" id="navbar">
            <ul class="nav navbar-nav">
                <li>
                    <a href="index.jsp">首 页</a>
                </li>
                <li>
                    <a href="game.jsp">比 赛</a>
                </li>
                <li>
                    <a href="rank.jsp">排 名</a>
                </li>
                <li>
                    <a href="about.jsp">通 知</a>
                </li>
            </ul>
            <ul class="nav navbar-nav navbar-right text-center">
                <%
                    LoginServer loginServer = new LoginServer();
                    loginServer.open();
                    Object cookie = session.getAttribute("cookie");
                    if (user == null || user.toString().equals("") || cookie == null || cookie.toString().equals("") || loginServer.checkLogin(user.toString(), cookie.toString())) {
                        session.removeAttribute("user");
                        session.removeAttribute("cookie");
                        loginServer.close();
                        session.setAttribute("infoTitle", "未登录");
                        session.setAttribute("infoContent", "您还未登录，请先登录");
                        session.setAttribute("return", "game.jsp");
                        session.setAttribute("panelColor", "red");
                        response.sendRedirect("information.jsp");
                        return;
                    } else {
                        GameServer gameServer = new GameServer();
                        gameServer.open();
                        int state = gameServer.getGameInformation().getInt("state");
                        gameServer.close();
                        if (state == 0) {
                            session.setAttribute("infoTitle", "信息");
                            session.setAttribute("infoContent", "比赛尚未开始");
                            session.setAttribute("return", "game.jsp");
                            session.setAttribute("panelColor", "red");
                            response.sendRedirect("information.jsp");
                        } else if (state == 2) {
                            session.setAttribute("infoTitle", "信息");
                            session.setAttribute("infoContent", "比赛已结束");
                            session.setAttribute("return", "rank.jsp");
                            session.setAttribute("panelColor", "red");
                            response.sendRedirect("information.jsp");
                        }
                %>
                <li>
                    <a href="mine" class="glyphicon glyphicon-user">&nbsp;<%=user%>
                    </a>
                </li>
                <li>
                    <a href="#" class="glyphicon glyphicon-log-out" data-toggle="modal"
                       data-target="#logout">&nbsp;退出</a>
                    <div class="modal fade" id="logout" role="dialog" aria-hidden="true"
                         aria-labelledby="logoutLabel">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"
                                            onclick="input_clear(document.getElementById('logout_form'))">&times;
                                    </button>
                                    <h4 class="modal-title" id="logoutLabel">退 出 登 录</h4>
                                </div>
                                <div class="modal-body">
                                    确定要退出吗？
                                    <form id="logout_form" action="logout" method="post">
                                        <input type="text" name="username" hidden="hidden" value="<%=user%>">
                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-primary"
                                            onclick="form_submit(document.getElementById('logout_form'))">&nbsp;退出
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </li>
                <%
                    }
                %>
            </ul>
        </div>
    </div>
</nav>

<%
    Decode decode = new Decode();
    RecordServer recordServer = new RecordServer();
    recordServer.open();
    LinkedList<String> accepted = recordServer.getUserAccepted((String) user);
    SubjectServer subjectServer = new SubjectServer();
    subjectServer.open();
    JSONArray subjectArray = subjectServer.getAllSubjectInformation();
%>
<div class="container text-center main">
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h1 class="panel-title">题目
            </h1>
        </div>
        <div class="panel-body">
            <%
                String state;
                int size = subjectArray.size() - 1;
                String subjectID;
                for (int i = 0; i <= size; i++) {
                    JSONObject subjectInfo = subjectArray.getJSONObject(i);
                    subjectID = subjectInfo.getString("uuid");
                    LinkedList<String> blood = recordServer.getBlood(subjectID);
                    LinkedHashMap<String, Integer> acceptedSubmitted = recordServer.getAcceptedSubmitted(subjectID);
                    if (accepted.contains(subjectID)) {
                        state = "（ 已通过 ）";
            %>
            <div class="panel panel-info">
                <%
                } else {
                    state = "（ 未通过 ）";
                %>
                <div class="panel panel-warning">
                    <%
                        }
                    %>
                    <div class="panel-heading">
                        <h2 class="panel-title"
                            style="font-size: 23px"><%=decode.languageDecode(subjectInfo.getString("title")).replace("\r", "<br>") + state%>
                        </h2>
                        <h4>
                            Accepted&nbsp;/&nbsp;Submitted:&nbsp;<%=acceptedSubmitted.get("acceptedCount") + " / " + acceptedSubmitted.get("submittedCount")%>
                        </h4>
                        <small style="font-size: 15px">First
                            Blood:&nbsp;<%=blood.get(0).equals("") ? "-" : blood.get(0)%>
                        </small>&nbsp;&nbsp;&nbsp;&nbsp;
                        <small style="font-size: 15px">Second
                            Blood:&nbsp;<%=blood.get(1).equals("") ? "-" : blood.get(1)%>
                        </small>&nbsp;&nbsp;&nbsp;&nbsp;
                        <small style="font-size: 15px">Third
                            Blood:&nbsp;<%=blood.get(2).equals("") ? "-" : blood.get(2)%>
                        </small>&nbsp;&nbsp;&nbsp;&nbsp;
                        <br>
                        <%
                            JSONArray answerLimit = JSONArray.fromObject(subjectInfo.getString("answer_limit"));
                            for (int j = 0; j < answerLimit.size(); j++) {
                                JSONObject limit = answerLimit.getJSONObject(j);
                        %>
                        &nbsp;&nbsp;&nbsp;&nbsp;
                        <small style="font-size: 15px">语言：<%=limit.getString("language")%>
                        </small>
                        <small style="font-size: 15px">时间：<%=limit.getLong("timeLimit")%> ms</small>
                        <small style="font-size: 15px">内存：<%=limit.getLong("memoryLimit")%> kb</small>
                        <%
                            }
                        %>
                    </div>
                    <div class="panel-body text-left" style="word-wrap:break-word;word-break:break-all;">
                        <div style="font-size: 18px;"><%=decode.languageDecode(subjectInfo.getString("content")).replace("\r", "<br>")%>
                        </div>
                        <br>
                    </div>
                    <div class="panel-footer">
                        <button class="btn btn-primary" data-toggle="modal"
                                data-target="#subject<%=subjectInfo.getString("uuid")%>" style="font-size: 16px">提交题目
                        </button>
                        <div class="modal fade" id="subject<%=subjectInfo.getString("uuid")%>" tabindex="-1"
                             role="dialog"
                             aria-labelledby="subject<%=subjectInfo.getString("uuid")%>Label" aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"
                                                onclick="code_clear(document.getElementById('code<%=subjectInfo.getString("uuid")%>'))">
                                            &times;
                                        </button>
                                        <h4 class="modal-title"
                                            id="subject<%=subjectInfo.getString("uuid")%>Label"><%=subjectInfo.getString("title")%>
                                        </h4>
                                    </div>
                                    <div class="modal-body">
                                        <form action="submit_subject" method="post"
                                              id="subject_<%=subjectInfo.getString("uuid")%>_form">
                                            <input type="text" name="subjectID"
                                                   value="<%=subjectInfo.getString("uuid")%>" hidden="hidden">
                                            <pre>
                                            <code>
                                                <textarea class="code_textarea java"
                                                          id="code<%=subjectInfo.getString("uuid")%>"
                                                          name="code"></textarea>
                                            </code>
                                        </pre>
                                            <br>
                                            <br>
                                            <hr>
                                            请选择语言
                                            <br>
                                            <br>
                                            <%
                                                UserServer userServer = new UserServer();
                                                userServer.open();
                                                String lastLanguage = userServer.getUserLastLanguage(user.toString());
                                                userServer.close();
                                                if (lastLanguage.equals("Java") || lastLanguage.equals("")) {
                                            %>
                                            <input type="radio" name="language" value="Java"
                                                   checked="checked">&nbsp;&nbsp;<span
                                                class="choice_language">Java</span>&nbsp;&nbsp;&nbsp;&nbsp;
                                            <input type="radio" name="language" value="C">&nbsp;&nbsp;<span
                                                class="choice_language">C</span>&nbsp;&nbsp;&nbsp;&nbsp;
                                            <input type="radio" name="language" value="CPP">&nbsp;&nbsp;<span
                                                class="choice_language">C++</span>&nbsp;&nbsp;&nbsp;&nbsp;
                                            <input type="radio" name="language" value="Python">&nbsp;&nbsp;<span
                                                class="choice_language">Python</span>&nbsp;&nbsp;&nbsp;&nbsp;
                                            <%
                                            } else if (lastLanguage.equals("C")) {
                                            %>
                                            <input type="radio" name="language" value="Java">&nbsp;&nbsp;<span
                                                class="choice_language">Java</span>&nbsp;&nbsp;&nbsp;&nbsp;
                                            <input type="radio" name="language" value="C"
                                                   checked="checked">&nbsp;&nbsp;<span
                                                class="choice_language">C</span>&nbsp;&nbsp;&nbsp;&nbsp;
                                            <input type="radio" name="language" value="CPP">&nbsp;&nbsp;<span
                                                class="choice_language">C++</span>&nbsp;&nbsp;&nbsp;&nbsp;
                                            <input type="radio" name="language" value="Python">&nbsp;&nbsp;<span
                                                class="choice_language">Python</span>&nbsp;&nbsp;&nbsp;&nbsp;
                                            <%
                                            } else if (lastLanguage.equals("CPP")) {
                                            %>
                                            <input type="radio" name="language" value="Java">&nbsp;&nbsp;<span
                                                class="choice_language">Java</span>&nbsp;&nbsp;&nbsp;&nbsp;
                                            <input type="radio" name="language" value="C">&nbsp;&nbsp;<span
                                                class="choice_language">C</span>&nbsp;&nbsp;&nbsp;&nbsp;
                                            <input type="radio" name="language" value="CPP"
                                                   checked="checked">&nbsp;&nbsp;<span
                                                class="choice_language">C++</span>&nbsp;&nbsp;&nbsp;&nbsp;
                                            <input type="radio" name="language" value="Python">&nbsp;&nbsp;<span
                                                class="choice_language">Python</span>&nbsp;&nbsp;&nbsp;&nbsp;
                                            <%
                                            } else {
                                            %>
                                            <input type="radio" name="language" value="Java">&nbsp;&nbsp;<span
                                                class="choice_language">Java</span>&nbsp;&nbsp;&nbsp;&nbsp;
                                            <input type="radio" name="language" value="C">&nbsp;&nbsp;<span
                                                class="choice_language">C</span>&nbsp;&nbsp;&nbsp;&nbsp;
                                            <input type="radio" name="language" value="CPP">&nbsp;&nbsp;<span
                                                class="choice_language">C++</span>&nbsp;&nbsp;&nbsp;&nbsp;
                                            <input type="radio" name="language" value="Python" checked="checked">&nbsp;&nbsp;<span
                                                class="choice_language">Python</span>&nbsp;&nbsp;&nbsp;&nbsp;
                                            <%
                                                }
                                            %>
                                        </form>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-danger"
                                                onclick="code_clear(document.getElementById('code<%=subjectInfo.getString("uuid")%>'))">
                                            清空
                                        </button>
                                        <button type="button" class="btn btn-primary"
                                                onclick="form_submit(document.getElementById('subject_<%=subjectInfo.getString("uuid")%>_form'))">
                                            提 交 代 码
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <%
                    }
                %>
            </div>
        </div>
    </div>

        <%
    subjectServer.close();
    recordServer.close();
    JSONArray foot = settingServer.readSettingArray("foot");
    JSONObject foot0 = foot.getJSONObject(0);
    JSONObject foot1 = foot.getJSONObject(1);
    JSONObject foot2 = foot.getJSONObject(2);
%>
    <div class="foot">
        <row>
            <div class="col-xs-4 col-sm-4">
                <br>
                <div class="foot_item_title">
                    <p><%=decode.utf8Decode(foot0.getString("title"))%>
                    </p>
                </div>
                <hr class="foot_split">
                <div class="foot_item">
                    <p><%=decode.utf8Decode(foot0.getString("line0"))%>
                    </p>
                    <p><%=decode.utf8Decode(foot0.getString("line1"))%>
                    </p>
                    <p><%=decode.utf8Decode(foot0.getString("line2"))%>
                    </p>
                </div>
            </div>
            <div class="col-xs-4 col-sm-4">
                <br>
                <div class="foot_item_title">
                    <p><%=decode.utf8Decode(foot1.getString("title"))%>
                    </p>
                </div>
                <hr class="foot_split">
                <div class="foot_item">
                    <p><%=decode.utf8Decode(foot1.getString("line0"))%>
                    </p>
                    <p><%=decode.utf8Decode(foot1.getString("line1"))%>
                    </p>
                    <p><%=decode.utf8Decode(foot1.getString("line2"))%>
                    </p>
                </div>
            </div>
            <div class="col-xs-4 col-sm-4">
                <br>
                <div class="foot_item_title">
                    <p><%=decode.utf8Decode(foot2.getString("title"))%>
                    </p>
                </div>
                <hr class="foot_split">
                <div class="foot_item">
                    <p><%=decode.utf8Decode(foot2.getString("line0"))%>
                    </p>
                    <p><%=decode.utf8Decode(foot2.getString("line1"))%>
                    </p>
                    <p><%=decode.utf8Decode(foot2.getString("line2"))%>
                    </p>
                </div>
            </div>
        </row>
    </div>
        <%
    settingServer.close();
%>
</body>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/page.js"></script>
<script src="js/ver_code.js"></script>
<script src="js/subject.js"></script>
</html>